
# coding:utf-8

from lib import flux, al
from base import config
from utils.suger import *
from lib.fluxext import singleton, ViewGroup
from system.game_time import GameTime


class LogWindow(ViewGroup):
    def __init__(self, line_num=7):
        anchor = flux.View()
        anchor.SetPos(0,0)
        anchor.SetSize(3, 3)
        anchor.SetColor(1,0,0)
        self.anchor = anchor

        height = 0
        lines = []

        for i in range(line_num):
            line = flux.RichTextView('wqyMedium')
            line.SetAnchor(anchor)
            line.SetColor(0,0,1)
            line.SetAlign(flux.ALIGN_TOPLEFT)
            line.SetText('')
            line.SetPos(0, height)
            height += line.GetSize().y
            vars(self)['line%s' % i] = line
            lines.append(line)

        self.SetHUD(True)
        self.lines = lines

        self.pos = -1
        self.line_num = line_num
        self.t = flux.Timer(3)
        def on_fire():
            if self.pos != -1:
                tmp = self.lines[0]
                tmp.SetVisible(False)
                del self.lines[0]
                self.lines.append(tmp)
                self.reset()
                self.pos -= 1
                if self.pos == -1:
                    self.t.Stop()
        self.t.OnFire = on_fire

    def reset(self):
        height  = 0
        for i in range(self.pos+1):
            line = self.lines[i]
            line.SetPos(0, height)
            height += line.GetSize().y

    def add(self, txt):
        if self.pos == -1:
            self.t.Start()

        self.pos += 1

        if self.pos == self.line_num:
            self.pos -= 1
            tmp = self.lines[0]
            del self.lines[0]
            self.lines.append(tmp)

        line = self.lines[self.pos]
        line.SetVisible(True)
        line.SetText(txt)
        self.reset()
